# plugs/core.py
#
#

""" core commands """

from ommbot.world import add_cmnd, add_example, get_cmnds, get_aliases

def handle_commands(bot, ievent):
    """ commands [<permission>] .. show commands allowed by permission .. \
        default permission is 'USER' """
    try:
        what = ievent.args[0].upper()
    except IndexError:
        what = 'USER'
    cmndlist = get_cmnds().list(what)
    cmndlist.sort()
    if cmndlist:
        ievent.reply('i have the following commands for the %s permission:\
 %s' % (what, ' '.join(cmndlist)))
    else:
        ievent.reply("can't find any command for permission %s" % what)

add_cmnd('commands', handle_commands, ['USER', 'WEB'])
add_example('commands', 'show commands allowed for [<permission>]', '1) \
             commands 2) commands OPER')

def handle_apro(bot, ievent):
    """ apro <cmnd> .. apropos for command """
    try:
        what = ievent.args[0]
    except:
        ievent.reply('apro <what>')
        return
    result = ""
    teller = 1
    for i in get_cmnds().apropos(what):
        result += '%s) %s ' % (str(teller), i)
        teller += 1
    result += 'aliases: '
    got = teller
    for i in get_aliases().data.keys():
        if what in i:
            result += '%s) %s ' % (teller, i)
            teller += 1
    if teller == got:
        result = result[:-9]
    if result:
        ievent.reply(result)
    else:
        ievent.reply('no matching commands found for %s' % what)

add_cmnd('apro', handle_apro, ['USER', 'WEB'])
add_example('apro', 'apro <what> .. search for commands that contain \
             <what>', 'apro com')
